<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="名称">
          <el-input v-model="form.name" placeholder="名称" />
        </el-form-item>
        <el-form-item label="渠道ID">
          <el-input v-model="form.channel_id" placeholder="渠道ID" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="channel_id"
            label="通道唯一ID"
            align="center"
          />
          <el-table-column
            prop="channel_code"
            label="通道Code"
            align="center"
          />
          <el-table-column label="通道图标" align="center">
            <template #default="scope">
              <el-image
                style="max-width: 220px; max-height: 100px"
                :src="scope.row.icon_show"
                alt
                fit="fill"
              />
            </template>
          </el-table-column>
          <el-table-column prop="name" label="通道名称" align="center" />
          <el-table-column prop="brief" label="通道简介" align="center" />
          <el-table-column prop="company" label="通道所属公司" align="center" />
          <el-table-column prop="category" label="通道分类" align="center" />
          <el-table-column
            prop="is_valid"
            label="是否生效"
            align="center"
            width="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_valid ? 'success' : 'danger'">{{
                scope.row.is_valid ? "已生效" : "未生效"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="is_refund"
            label="是否允许退款"
            align="center"
            width="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_refund ? 'success' : 'danger'">{{
                scope.row.is_refund ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            :formatter="formatterTime"
            width="center"
          />
          <!-- <el-table-column prop="rate" label="限额" align="center" /> -->
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150px"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <!--<el-button type="danger" @click="handleOperate(scope.$index, scope.row, 'delete')">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleRemove="handleRemove"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :supplierList="supplierList"
      @handleUploaderImgRequest="handleUploader"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  channelListApi,
  channelAddApi,
  channelUpdateApi,
} from "@/api/payServerse/settings";
import DialogForm from "./components/paymentList.vue";
const loading = ref(false);
const tableData = ref([]);
const supplierList = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const formData = {
  name: "",
  channel_id: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  channel_id: "",
  channel_code: "",
  is_valid: "",
  is_refund: "",
  name: "",
  brief: "",
  company: "",
  category: "",
  icon_show: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
    channel_id: form.channel_id,
  };
};
// 获取
const getSupplierList = async () => {
  try {
    const res = await channelListApi({});
    supplierList.value = res.data;
  } catch (error) {
    supplierList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await channelListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
//上传图标
const handleUploader = (res) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    dialogForm.icon = res.data.name || "";
    dialogForm.icon_show = res.data.pic_url || "";
  }
};
// 提交
const submit = async (param) => {
  delete param.src;
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await channelAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      console.log(param);
      const res = await channelUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogVisible.value = true;
    dialogForm.id = row.id;
    dialogForm.is_valid = row.is_valid;
    dialogForm.is_refund = row.is_refund;
    dialogForm.name = row.name;
    dialogForm.brief = row.brief;
    dialogForm.channel_id = row.channel_id;
    dialogForm.channel_code = row.channel_code;
    dialogForm.company = row.company;
    dialogForm.category = row.category;
    dialogForm.icon = row.icon;
    dialogForm.icon_show = row.icon_show;
  }
  // // 删除
  //  if (type === "delete") {
  //    ElMessageBox.confirm("您是否确认删除此支付渠道?", "温馨提示", {
  //      type: "warning"
  //    }).then(async () => {
  //      const res = await channelDeleteApi({ id: row.id });
  //      getTbaleData();
  //      ElMessage.success(res.message);
  //    });
  //  }
};

const handleRemove = () => {
  dialogForm.icon = "";
  dialogForm.icon_show = "";
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  //  getSupplierList();
});
</script>
